package com.bw.order.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bw.order.vo.HouseUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.bw.order.entity.HousePurchaseOrderEntity;
import com.bw.order.service.HousePurchaseOrderService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;


/**
 * 购买订单表
 *
 * @author Hao_YunKai
 * @email 2745578447@qq.com
 * @date 2022-03-23 16:04:29
 */
@RestController
@RequestMapping("order/house-purchase-order")
public class HousePurchaseOrderController {
    @Autowired
    private HousePurchaseOrderService housePurchaseOrderService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = housePurchaseOrderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{purchaseOid}")
    public R info(@PathVariable("purchaseOid") Long purchaseOid){
		HousePurchaseOrderEntity housePurchaseOrder = housePurchaseOrderService.getById(purchaseOid);

        return R.ok().put("housePurchaseOrder", housePurchaseOrder);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestParam Map<String, Object> params){
        R add = housePurchaseOrderService.add(params);

        return add;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HousePurchaseOrderEntity housePurchaseOrder){
		housePurchaseOrderService.updateById(housePurchaseOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] purchaseOids){
		housePurchaseOrderService.removeByIds(Arrays.asList(purchaseOids));

        return R.ok();
    }
    @RequestMapping("getUrl")
    public R getUrl(@RequestParam Map<String, Object> params){
        String id = (String) params.get("id");
        String uids = (String) params.get("uuid");
        String uuid = UUID.randomUUID().toString().replace("-","");
        String s = null;
        try {
            s = redisTemplate.opsForValue().get(uids);
        } catch (Exception e) {
            System.out.println("登录失效");
            return R.error(403,"登录失效");
        }
        if (s!=null && !s.equals("")){
            HouseUserVo vo = JSONObject.parseObject(s, new TypeReference<HouseUserVo>() {
            });
            redisTemplate.opsForValue().set(uuid+vo.getUserPhone(),id+"",5, TimeUnit.MINUTES);
            return R.ok().put("uuids",uuid);
        }  else {
            return R.error(403,"登录失效");
        }
    }

}
